package com.xuan.controller;

import com.xuan.entity.Orders;
import com.xuan.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xuan
 * @since 2025-01-14
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;
    @GetMapping("/{id}")
    public Orders getOrder(@PathVariable Long id) {
        return iOrdersService.getById(id);  // 通过订单 ID 获取订单
    }

    @PostMapping
    public boolean addOrder(@RequestBody Orders order) {
        return iOrdersService.save(order);  // 调用服务层的方法进行插入
    }

    @PutMapping("/{id}")
    public boolean updateOrder(@PathVariable Long id, @RequestBody Orders order) {
        order.setId(id);  // 确保更新时使用正确的 ID
        return iOrdersService.updateById(order);  // 调用服务层的更新方法
    }

    @DeleteMapping("/{id}")
    public boolean deleteOrder(@PathVariable Long id) {
        return iOrdersService.removeById(id);  // 调用服务层的删除方法
    }

}
